From: shand@ubuntu.eng.hq.xensource.com Date: Thu, 15 Sep 2005 01:46:40 +0000 (-0800) Subject: O_REUSEADDR is not enough to ensure we don't get address in use errors when X-Git-Tag: archive/raspbian/4.8.0-1+rpi1~1^2~16780^2~81^2~2 X-Git-Url: https://dgit.raspbian.org/%22http://www.example.com/cgi/%22/%22http:/www.example.com/cgi/%22?a=commitdiff_plain;h=e739a4d453d6bc578d24dd21a1054e77351075b4;p=xen.git O_REUSEADDR is not enough to ensure we don't get address in use errors when xend dies from a signal. We implement a 30 second time to ensure we don't fail if we don't have to. Signed-off-by: Anthony Liguori --- diff --git a/tools/python/xen/web/tcp.py b/tools/python/xen/web/tcp.py index ed57d45159..157d059540 100644 --- a/tools/python/xen/web/tcp.py +++ b/tools/python/xen/web/tcp.py @@ -18,6 +18,7 @@ import sys import socket import types +import time from connection import * from protocol import * @@ -35,8 +36,25 @@ class TCPListener(SocketListener): def createSocket(self): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) - addr = (self.interface, self.port) - sock.bind(addr) + + # SO_REUSEADDR does not always ensure that we do not get an address + # in use error when restarted quickly + # we implement a timeout to try and avoid failing unnecessarily + + timeout = time.time() + 30 + again = True + while again and time.time() < timeout: + again = False + try: + sock.bind((self.interface, self.port)) + except socket.error, (errno, strerrno): + if errno == 98: + again = True + else: + raise socket.error(errno, strerrno) + if again: + raise socket.error(98, "address in use") + return sock def acceptConnection(self, sock, protocol, addr):